package com.fanli.scheduler.serviceImpl;

import com.fanli.scheduler.entity.EtlMetaColumn;
import com.fanli.scheduler.entity.EtlMetaColumnExample;
import com.fanli.scheduler.entity.EtlMetaTableBase;
import com.fanli.scheduler.entity.EtlMetaTableBaseExample;
import com.fanli.scheduler.mapping.EtlMetaColumnMapper;
import com.fanli.scheduler.mapping.EtlMetaTableBaseMapper;
import com.fanli.scheduler.service.HiveMetaService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xu.liu on 2016/9/5.
 */
@Service
public class HiveMetaServiceImpl implements HiveMetaService {

    @Autowired
    private EtlMetaTableBaseMapper baseMapper;
    @Autowired
    private EtlMetaColumnMapper columnMapper;
    @Override
    public List<EtlMetaTableBase> getEnableMetaTableBasesByDb(String db) {
        EtlMetaTableBaseExample example=new EtlMetaTableBaseExample();
        EtlMetaTableBaseExample.Criteria criteria=example.createCriteria();
        example.setOrderByClause("table_name asc");
        if (db!=null){
            criteria.andDbEqualTo(db);
            byte b=1;
            criteria.andStatusEqualTo(new Byte(b));
        }
        List<EtlMetaTableBase> items=baseMapper.selectByExample(example);
//        List<String> result= null;
//        if(CollectionUtils.isNotEmpty(items)){
//            result=new ArrayList<String>();
//            for (EtlMetaTableBase item:items) {
//                result.add(item.getGlobalTableId()+"--"+item.getTableName());
//            }
//        }
        return items;
    }

    @Override
    public List<EtlMetaColumn> getAllEnableByTid(String id) {
        EtlMetaColumnExample example=new EtlMetaColumnExample();
        EtlMetaColumnExample.Criteria criteria=example.createCriteria();
        if(id!=null){
            criteria.andGlobalIdEqualTo(Long.parseLong(id));
            example.setOrderByClause("column_index");
        }
        return columnMapper.selectByExample(example);
    }
}
